package com.bo.service.impl;

import com.bo.mapper.EmpMapper;
import com.bo.pojo.Emp;
import com.bo.pojo.PageBean;
import com.bo.service.EmpService;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @author huangbo
 * @version 1.0.0
 */
@Service
public class EmpServiceImpl implements EmpService {
    @Autowired
    private EmpMapper empMapper;

    //分页查询
    @Override
    public PageBean page(Integer page, Integer pageSize, String name, Short gender, LocalDate begin, LocalDate end) {
//        Long count=empMapper.count();   //总记录数
//        Integer start = (page - 1) * pageSize ; //分页起点索引
//        List<Emp> empList = empMapper.page(start, pageSize); //分页查询结果列表
//        PageBean pageBean = new PageBean(count, empList);
//        return pageBean;
        PageHelper.startPage(page,pageSize);    //设置分页参数
        List<Emp> empList = empMapper.list(name, gender, begin, end);
        Page<Emp> p = (Page<Emp>) empList;

        PageBean pageBean = new PageBean(p.getTotal(), p.getResult());
        return pageBean;
    }

    //批量删除
    @Override
    public void delete(Integer[] ids) {
        empMapper.delete(ids);
    }

    //添加员工
    @Override
    public void insert(Emp emp){
        emp.setCreateTime(LocalDateTime.now());
        emp.setUpdateTime(LocalDateTime.now());//补充基础属性
        empMapper.insert(emp);
    }

    //查询员工
    @Override
    public Emp get(Integer id){
        return empMapper.get(id);
    }

    //修改员工
    @Override
    public void change(Emp emp){
        emp.setUpdateTime(LocalDateTime.now());
        empMapper.change(emp);
    }

    //登录
    @Override
    public Emp login(Emp emp){
       return empMapper.getByUsernameAndPassword(emp);
    }

    @Override
    public void deletepro(Integer deptId) {
        empMapper.deletepro(deptId);
    }
}

